package com.metarnet.topo.dataaccess.factory;

import java.io.FileInputStream;
import java.util.List;
import javax.sql.RowSet;

import com.metarnet.topo.dataaccess.abs.AbstractDataAccessFacade;
import com.metarnet.topo.dataaccess.oracle.OracleDataAccessFacade;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 创建操作跨数据库的外观类
 * @author 曹超杰
 */
public class DataAccessFacadeCreator {

	private static DataAccessFacadeCreator instance;
	public static String dbType;
	private static String dbDriver;
	private static String url;
	private static String userName;
	private static String password;

	/**
	 * 获取配置文件中连接字符串
	 * 静态函数
	 * @return instance
	 * @throws ConfigurationException
	 */
	@SuppressWarnings("unchecked")
	public static DataAccessFacadeCreator getInstance() {
		if (instance == null) {
			try{
				String webPath = "F:/开发小项目/Topo/METARNET/trunk/NetTopo/WebRoot/WEB-INF/";
				String file = "dbConfig.xml";
				SAXReader sax = new SAXReader();
				Document doc = sax.read(new FileInputStream(webPath + file));
				Element root = doc.getRootElement();
				
				if(root != null){
		        	Attribute attribute = root.attribute("id");
	            	
	            	if(attribute != null && "dataSource".equals(attribute.getValue())){
			            // 读取dbConfig.xml中dateSource
	            		List<Element> list = doc.selectNodes(root.getUniquePath()+"/property");
			            if (list != null) {
			                for (Element el : list) {
			                    Attribute attr = el.attribute("name");
			                    if("driverClassName".equals(attr.getValue())){
			                    	 List<Element> valueList = doc.selectNodes(el.getUniquePath()+"/value");
			                    	 if(valueList!=null){
			                    		 Element ele = valueList.get(0);
			                    		 dbDriver = ele.getStringValue();
			                    	 }
			                    }else if("url".equals(attr.getValue())){
			                    	 List<Element> valueList = doc.selectNodes(el.getUniquePath()+"/value");
			                    	 if(valueList!=null){
			                    		 Element ele = valueList.get(0);
			                    		 url = ele.getStringValue();
			                    	 }
			                    }else if("username".equals(attr.getValue())){
			                    	 List<Element> valueList = doc.selectNodes(el.getUniquePath()+"/value");
			                    	 if(valueList!=null){
			                    		 Element ele = valueList.get(0);
			                    		 userName = ele.getStringValue();
			                    	 }
			                    }else if("password".equals(attr.getValue())){
			                    	 List<Element> valueList = doc.selectNodes(el.getUniquePath()+"/value");
			                    	 if(valueList!=null){
			                    		 Element ele = valueList.get(0);
			                    		 password = ele.getStringValue();
			                    	 }
			                    }
			                }
			            }
	               }
	            }
	            System.out.println(dbDriver+"\n"+url+"\n"+userName+" : "+password);
	            instance = new DataAccessFacadeCreator();
	            
			}catch(Exception ex){
				ex.printStackTrace();
			}

		}
		return instance;
	}

	/**
	 * 创建带参数的数据库外观类
	 * @param facadeName
	 */
	public AbstractDataAccessFacade createDataAccessFacade(String facadeName,String dbDriver, String url, String userName, String password) {
		String fName = facadeName.toLowerCase();
		AbstractDataAccessFacade returnFacade = null;
		if (fName.equalsIgnoreCase("org.hibernate.dialect.OracleDialect")) {
			System.out.println("------------dbType: Oracle");
			returnFacade = new OracleDataAccessFacade(dbDriver, url, userName,password);
		} else if (fName.equalsIgnoreCase("org.hibernate.dialect.MySQLDialect")) {
			System.out.println("------------dbType: MySQL");
			
		} else if (fName.equalsIgnoreCase("org.hibernate.dialect.SQLServerDialect")) {
			System.out.println("------------dbType: sqlServer");
			
		} else {
			returnFacade = new OracleDataAccessFacade(dbDriver, url, userName,password);
		}
		return returnFacade;
	}

	/**
	 * 创建默认的数据库外观类
	 * @return facadeName
	 * @throws ConfigurationException
	 */
	public AbstractDataAccessFacade createDataAccessFacade() {
		return createDataAccessFacade(dbType, dbDriver, url, userName, password);
	}
	
	/**
	 * Test DB
	 */
	public static void main(String[] args){
		DataAccessFacadeCreator dafc = DataAccessFacadeCreator.getInstance();
		dbType = "oracle";
		AbstractDataAccessFacade adaf = dafc.createDataAccessFacade();
		try{
			RowSet crs = adaf.getRowSet("select * from userinfo");
			System.out.println("\nuserid\tusername");
			while(crs.next()){
				String id = crs.getString("userid");
				String name = crs.getString("username");
				System.out.println(id+"\t"+name);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}